//
//  CalculatorBrain.m
//  Calculator
//
//  Created by Hilary Muludiang on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalculatorBrain.h"


@implementation CalculatorBrain

- (void)setOperand:(double)aDouble
{
	operand=aDouble;
	
}
- (double)setSign:(double)aDouble
{
	operand = -(aDouble);
	return operand;
}

- (void)performWaitingOperation
{
	if ([@"+" isEqual:waitingOperation])
	{
		operand=waitingOperand+operand;
	}else if ([@"*" isEqual:waitingOperation])
	{
		operand=waitingOperand*operand;
	}else if ([@"-" isEqual:waitingOperation])
	{
		operand=waitingOperand-operand;
	}else if ([@"/" isEqual:waitingOperation])
	{
		if(operand)
		{
			operand=waitingOperand/operand;
		}
	}
	
}

- (double)performOperation:(NSString *)operation
{
	
	if ([operation isEqual:@"Store"])
	{
		valueInMemory = operand;
	}
	else if ([operation isEqual:@"Mem+"])
	{	
		valueInMemory =  valueInMemory+operand;
	}
	else if ([operation isEqual:@"Recall"])
	{
		operand = valueInMemory;;
	}
	else if ([operation isEqual:@"Cos"])
	{
		operand = cos(operand);
	}
	else if ([operation isEqual:@"Sin"])
	{
		operand = sin(operand);
	}
	else if ([operation isEqual:@"sqrt"])
	{
		operand = sqrt(operand);
	}
	else if ([operation isEqual:@"1/x"] && !operand == 0)
	{
		operand = (1/operand);
	}
	else if ([@"+/-"isEqual:operation])
	{
		operand = - operand;
	}
	else
	{
		[self performWaitingOperation];
		waitingOperation = operation;
		waitingOperand = operand;
	}
	return operand;
}

-(void)resetCalculator
{
	operand = 0;
	waitingOperand = 0;
	waitingOperation = nil;
	valueInMemory = 0;
}
@end
